import { MailerModule } from '@nestjs-modules/mailer';
import { EjsAdapter } from '@nestjs-modules/mailer/dist/adapters/ejs.adapter';
import { DynamicModule, Module } from '@nestjs/common';
import { QQEmailService } from './email.service';
import { QQEmailController } from './email.controller';

export interface QQEmailOptions {
  auth: { user: string; pass: string };
  from: string;
  templateDir: string;
}

@Module({})
export class QQEmailModule {
  static register(options: QQEmailOptions): DynamicModule {
    return {
      module: QQEmailModule,
      imports: [
        MailerModule.forRoot({
          transport: {
            host: 'smtp.qq.com', //邮箱服务器地址
            port: 465, //服务器端口 默认 465
            auth: {
              user: options.auth.user, //你的邮箱地址
              pass: options.auth.pass,
            },
            domains: [
              // 后缀的域名
              'qq.com',
            ],
          },
          preview: true, //是否开启预览，开启了这个属性，在调试模式下会自动打开一个网页，预览邮件
          defaults: {
            from: options.from, //发送人 你的邮箱地址
          },
          template: {
            dir: options.templateDir, //这里就是你的ejs模板文件夹路径
            adapter: new EjsAdapter(),
            options: {
              strict: true, //严格模式
            },
          },
        }),
      ],
      providers: [QQEmailService],
      controllers: [QQEmailController],
    };
  }
}
